import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

// Vuex 持久化  
import createPersistedState from "vuex-persistedstate";   // localStorage 
import { ajax } from './../api/index';


export default new Vuex.Store({
  plugins:[
      createPersistedState()
  ],
  state: {
    cartNum:9,
    userInfo:null, // 用户信息 判断是否登录 
    city:null,  // 当前的城市信息 
    cityList:[],  // 中国所有的城市
    allCinemas:[],  // 当前城市的电影院  city改变重新请求数据 
  },
  mutations: {
    changeUserInfo(state,payload){
      state.userInfo = payload;
    },
    changeCity(state,payload){
      state.city = payload 
    },
    changeCityList(state,payload){
      state.cityList = payload 
    },
    changeAllCinemas(state,payload){
      state.allCinemas = payload; 
    }
  },
  getters:{
    hotcity(state){
      return state.cityList.filter(item=>item.isHot==1)
    }
  },
  actions: {
    async getCinemasAsync({commit},cityId){
      let {data} = await  ajax.getMZdata({
            cityId,
        },{
            'X-Host': 'mall.film-ticket.cinema.list'
        })
        commit('changeAllCinemas',data.cinemas)
    }
  },
  modules: {
  }
})
